/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.front.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import me.zhengjie.front.domain.OutboundOrderDetailFront;
import me.zhengjie.front.domain.vo.OutboundOrderDetailQueryCriteria;
import me.zhengjie.front.mapper.OutboundOrderDetailFrontMapper;
import me.zhengjie.front.service.OutboundOrderDetailService;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.PageUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @description 服务实现
* @author XX
* @date 2024-04-29
**/
@Service
@RequiredArgsConstructor
public class OutboundOrderDetailServiceImpl extends ServiceImpl<OutboundOrderDetailFrontMapper, OutboundOrderDetailFront> implements OutboundOrderDetailService {

    private final OutboundOrderDetailFrontMapper outboundOrderDetailFrontMapper;

    @Override
    public PageResult<OutboundOrderDetailFront> queryAll(OutboundOrderDetailQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(outboundOrderDetailFrontMapper.findAll(criteria, page));
    }

    @Override
    public List<OutboundOrderDetailFront> queryAll(OutboundOrderDetailQueryCriteria criteria){
        return outboundOrderDetailFrontMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(OutboundOrderDetailFront resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(OutboundOrderDetailFront resources) {
        OutboundOrderDetailFront outboundOrderDetailFront = getById(resources.getId());
        outboundOrderDetailFront.copy(resources);
        saveOrUpdate(outboundOrderDetailFront);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<OutboundOrderDetailFront> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (OutboundOrderDetailFront outboundOrderDetailFront : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("出库单号", outboundOrderDetailFront.getOutboundOrderCode());
            map.put("需求数", outboundOrderDetailFront.getRequireNum());
            map.put("正在执行数", outboundOrderDetailFront.getStockUpNum());
            map.put("完成数", outboundOrderDetailFront.getCompleteNum());
            map.put("状态", outboundOrderDetailFront.getStatus());
            map.put("大储位", outboundOrderDetailFront.getSuggestLargeStorage());
            map.put("中储位", outboundOrderDetailFront.getSuggestMediumStorage());
            map.put("小储位", outboundOrderDetailFront.getSuggestSmallStorage());
            map.put(" outboundDetailId",  outboundOrderDetailFront.getOutboundDetailId());
            map.put("物料编码", outboundOrderDetailFront.getMaterialCode());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}